package com.syn.synold4s.control;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.gaoyi.basemodule.baseControl.BaseView;
import com.syn.synold4s.R;
import com.syn.synold4s.databinding.MainTitleViewBinding;
import com.syn.synold4s.util.MyAnimUtil;

public class MainTitleView extends BaseView<MainTitleViewBinding> {

    private View mAnimView;

    public void setAnimView(View view) {
        this.mAnimView = view;
    }

    public MainTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void initView(TypedArray ta) {
        String title = ta.getString(R.styleable.MainTitleView_main_title_text);
        setTitle(title);
        mBinding.mainTitleExpandIv.setOnClickListener(view -> {
            boolean isCollapse = !mBinding.mainTitleExpandIv.isSelected();
            mBinding.mainTitleExpandIv.setSelected(isCollapse);
            startAnim(mAnimView,isCollapse);
        });
    }

    private void startAnim(View view, boolean isCollapse) {
        if (view == null) {
            return;
        }
        MyAnimUtil.startAnim(view,isCollapse);
    }

    /**
     * 标题
     */
    public void setTitle(String title) {
        mBinding.mainTitle.setTitle(title);
    }

    public void setAllDevice(int count, int total) {
        mBinding.mainAllDevice.setText(String.format(mContext.getString(R.string.all_device_s), count, total));
    }

    public void setStatus(boolean isSuccess) {
        mBinding.mainTitleStatusIv.setImageResource(isSuccess ? R.drawable.bg_oval_green : R.drawable.bg_oval_red);
    }

    @Override
    protected MainTitleViewBinding getViewBinding() {
        return MainTitleViewBinding.inflate(LayoutInflater.from(mContext));
    }

    @Override
    protected int[] getAttrs() {
        return R.styleable.MainTitleView;
    }
}
